/**
 * Created by Administrator on 2017/4/26 0026.
 */
var DB =  require('../db/db');

/**发布评论 添加到数据库*/
exports.releaseComment = function(req,res,msg){
    var id = req.body.id;
    var user_id = req.body.user_id;
    var goods_id = req.body.goods_id;
    var content = req.body.content;
    var add_time = req.body.add_time;
    var photo = req.body.photo;
    var sql = "INSERT into k_comment(Id,user_id,content,add_time,photo) VALUES ('"
        + id +"',"
        +"'" + user_id +"',"
        +"'"+ content +"',"
        + add_time +","
        +"'"+ photo +"')";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg = er;
            res.json(msg);
        }
    });
};

/**根据uid获取用户评论*/
exports.getCommentById= function(req,res,msg){
    var user_id = req.body.user_id;
    if(user_id){
        var sql="SELECT * from k_comment where user_id ='"+user_id+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----" + JSON.stringify(list));
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    };
};

//deleteCommentById
exports.deleteCommentById= function(req,res,msg){
    var Id = req.body.Id;
    if(Id){
        var sql="DELETE from k_comment where Id ='"+Id+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----" + JSON.stringify(list));
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    };
};

/**获取用户评论按时间排序*/
exports.getTiyanInfo= function(req,res,msg){
    var Id = req.body.Id;
    if(Id){
        var sql="SELECT * from k_comment WHERE Id ='"+Id+"'";
    }else{
        var sql="SELECT * from k_comment order by add_time desc";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----" + JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });

};
/**点赞*/
exports.UpdateZan = function(req,res,msg){
    var user_id = req.body.user_id;
    var comment_id = req.body.comment_id;
    if(user_id){
        var sql="SELECT * FROM k_comment_user WHERE user_id = '"+user_id+"' AND comment_id ='"+comment_id+"' AND is_zan = 1";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er&list.length>0){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                var sql2="INSERT INTO k_comment_user(user_id,comment_id,is_zan) VALUES('"+user_id+"','"+comment_id+"','1')";
                console.log(sql2);
                DB.query(sql2,function(er,list){
                    if(!er){
                        var sql3="UPDATE k_comment SET zan_number = zan_number + 1 WHERE Id ='"+comment_id+"'";
                        console.log(sql3);
                        DB.query(sql3,function(er,list){
                            if(!er){
                                msg.success=true;
                                msg.data = list;
                                res.json(msg);
                            }else{
                                msg.msg=er;
                                res.json(msg);
                            }
                        });
                    }else{
                        msg.msg=er;
                        res.json(msg);
                    }
                });
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    };
};

/**获取评论*/
exports.getComment = function(req,res,msg){
    var comment_id = req.body.comment_id;
    var sql="SELECT cu.*,u.photo,u.nickname,u.account FROM k_comment_user as cu LEFT JOIN k_user as u ON u.id = cu.user_id WHERE comment_id ='"+comment_id+"' order by cu.time desc";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er) {
            msg.success = true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**评论*/
exports.insertComment = function(req,res,msg){
    var user_id = req.body.user_id;
    var comment_id = req.body.comment_id;
    var comment = req.body.comment;
    var time = req.body.time;
    if(user_id){
        var sql2="INSERT INTO k_comment_user(user_id,comment_id,comment,time) VALUES('"+user_id+"','"+comment_id+"','"+comment+"','"+time+"')";
        console.log(sql2);
        DB.query(sql2,function(er,list){
            if(!er){
                var sql3="UPDATE k_comment SET comment_number = comment_number + 1 WHERE Id ='"+comment_id+"'";
                console.log(sql3);
                DB.query(sql3,function(er,list){
                    if(!er){
                        msg.success = true;
                        msg.msg = "评论更新";
                        res.json(msg);
                    }else{
                        msg.msg=er;
                        res.json(msg);
                    }
                });
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    };
};
